package com.ucode.springboot.starter.security.provider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import com.ucode.springboot.starter.security.entity.UcodeUser;
import com.ucode.springboot.starter.security.entity.UcodeUserDetails;
import com.ucode.springboot.starter.security.service.UcodeUserService;
import com.ucode.tool.base.ResultCodeEnum;

/**
 * 用户登录认证信息查询
 * @author: liliang
 * @date: 2019年12月19日 下午8:43:33
 */
@Component
public class UcodeUserDetailsService implements UserDetailsService{

    @Autowired
    private UcodeUserService securityUserService;
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //获取登陆用户信息
        UcodeUser user = securityUserService.getUser(username);
        if(user == null)
            throw new UsernameNotFoundException(ResultCodeEnum.USER_UNREGISTERED.message);
        return new UcodeUserDetails(user);
    }
}
